﻿using System;
using System.IO;
using System.Reflection;
using System.Security;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using DZTC.Base32Utility.Core;
using Microsoft.Win32;

namespace DZTC.Base32Utility.Installer
{
    internal static class Program
    {
        [STAThread]
        private static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                ShowUsage();
                return;
            }

            switch (args[0].ToLower())
            {
                case "--install":
                    InstallApplication();
                    break;

                case "--uninstall":
                    UninstallApplication();
                    break;

                default:
                    ShowUsage();
                    return;
            }

        }

        private static string GetCommand(string appendix)
        {
            var module = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "base32.exe");
            return String.Format("\"{0}\" {1}", module, appendix);
        }

        public static void UninstallApplication()
        {
            
                try
                {
                    Registry.ClassesRoot
                        .OpenSubKey("*", true)
                        .OpenSubKey("shell", true)
                        .DeleteSubKeyTree(Const.PROG_ID);
                }
                catch (ArgumentException ex)
                {
                    // supress
                }

                try
                {
                    Registry.ClassesRoot.DeleteSubKeyTree(Const.EXTENSION);
                }
                catch (ArgumentException ex)
                {
                    // supress
                }

                try
                {
                    Registry.ClassesRoot.DeleteSubKeyTree(Const.PROG_ID);
                }
                catch (ArgumentException ex)
                {
                    // supress
                }
            
        }

        public static void InstallApplication()
        {
            var encodeTo = Registry.ClassesRoot.OpenSubKey("*", true)
                .OpenSubKey("shell", true)
                .CreateSubKey(Const.PROG_ID);

            encodeTo.SetValue(null, "Encode to BASE32");
            encodeTo.CreateSubKey("command").SetValue("", GetCommand("--encode %1"));

            var progId = Registry.ClassesRoot.CreateSubKey(Const.PROG_ID);

            progId.CreateSubKey("DefaultIcon")
                .SetValue("", GetCommand(",0"));

            progId.CreateSubKey("shell")
                .CreateSubKey("open")
                .CreateSubKey("command")
                .SetValue("", GetCommand("--decode %1"));


            Registry.ClassesRoot.CreateSubKey(Const.EXTENSION)
                .SetValue("", Const.PROG_ID);
        }

        private static void ShowUsage()
        {
            MessageBox.Show("installer --install - install shell integration\n" +
                            "installer --uninstall - uninstall shell instegration",
                            "usage");
        }
    }
}
